<?php

namespace Custom\Controller;

use Business\StrategyFactory;
use Common\Common\GlobalParam;
use Common\Controller\BaseController;
use Think\Page;


class IndexController extends BaseController
{
    public function index()
    {
        $iCustomStrategy = StrategyFactory::create_customStrategy($this);
        $iCustomStrategy->index();
        $this->display($iCustomStrategy->view_path());
    }

    public function sea()
    {
        $iCustomStrategy = StrategyFactory::create_customStrategy($this);
        $iCustomStrategy->sea();
        $this->display($iCustomStrategy->view_path($view_name = 'sea'));
    }

    public function signed()
    {
        $iCustomStrategy = StrategyFactory::create_customStrategy($this);
        $iCustomStrategy->signed();
        $this->display();
    }

    public function depart()
    {
        $this->assign('Custom_type', C('Custom_type'));

        $model = D('CrmCustom');
        $sp = $_GET['p'];
        if (empty($sp)) {
            $sp = 1;
        }
        $search_key = $_GET['search_key'];
        $search_type = $_GET['search_type'];
        $search_distribute = $_GET['search_distribute'];

        $user_id = $this->current_user['id'];
        $user_ids = fc_get_employee_ids_by_uid($user_id);
        $list = $model->get_list($user_ids, $search_key, $search_type, $search_distribute, $sp, C('PAGE_SIZE'));

        $page = new Page($list[1], C('PAGE_SIZE'));

        $this->assign('total_rows', $page->totalRows);
        $this->assign('_page', $page);
        $this->assign('page_link', $page->show());
        $this->assign('custom_list', $list[0]);

        foreach ($_GET as $key => $val) {
            $page->parameter[$key] = urlencode($val);
        }
        $this->display();
    }

    public function edit()
    {
        $this->display();
    }

    public function detail()
    {
        $id = $_GET['id'];
        $model = D('CrmCustom');
        $custom = $model->get($id);

        $this->assign('custom', $custom);
        $numbers = $this->current_user['numbers'];

        $this->assign('numbers', $numbers);
        $this->display();
    }

    public function save()
    {
        $model = D('CrmCustom');
        $data = $model->create();

        if (!$data) { //数据对象创建错误
            echo '数据对象创建错误';
        } else {
            if (empty($data['id'])) {
                $data['id'] = null;
                $data['state'] = GlobalParam::$Custom_State_Distributed;
                $data['leader_user_id'] = $this->current_user['id'];
                $data['add_user_id'] = $this->current_user['id'];
            }

            $result = $model->update($data);
            if (!$result && $result != '') {
                echo $model->getError();
            } else {
                $contactname = $_POST['contactname'];
                $contactjob = $_POST['contactjob'];
                $contactphone = $_POST['contactphone'];
                $contactpretel = $_POST['contactpretel'];
                $contacttel = $_POST['contacttel'];
                $cmodel = D('CrmCustomContact');
                for ($i = 0; $i < count($contactname); $i++) {
                    if (!empty($contactname[$i])) {
                        $cdata = $cmodel->create();
                        $cdata['id'] = null;
                        $cdata['name'] = $contactname[$i];
                        $cdata['job'] = $contactjob[$i];
                        $cdata['phone'] = $contactphone[$i];
                        $cdata['tel'] = $contactpretel[$i] . $contacttel[$i];
                        $cdata['custom_id'] = $result;
                        $cdata['add_user_id'] = $this->current_user['id'];
                        $cmodel->add($cdata);
                    }
                }
                echo '0';
            }
        }
        exit();
    }

    public function delete()
    {
        $id = $_GET['id'];

        $model = D('CrmCustom');
        $result = $model->delete($id, $this->current_user['id']);
        if (!$result) {
            echo $model->getError();
        } else {
            echo '0';
        }
        exit();
    }

    public function saveContact()
    {
        $model = D('CrmCustomContact');
        $data = $model->create();
        if (!$data) { //数据对象创建错误
            echo '数据对象创建错误';
        } else {
            if (empty($data['id'])) {
                $data['id'] = null;
                $data['add_user_id'] = $this->current_user['id'];
            }

            $data = $model->update($data);
            if (!$data) {
                echo $model->getError();
            } else {
                echo '0';
            }
        }
        exit();
    }

    public function editContact()
    {
        $this->display();
    }

    public function deleteContact()
    {
        $id = $_GET['id'];
        $model = D('CrmCustomContact');
        $result = $model->delete($id);
        if (!$result) {
            echo $model->getError();
        } else {
            echo '0';
        }
        exit();
    }

    public function saveActivity()
    {
        $model = D('CrmActivity');
        $data = $model->create();

        if (!$data) { //数据对象创建错误
            echo '数据创建错误';
        } else {
            if (empty($data['activity_time'])) {
                $data['activity_time'] = date('Y-m-d H:i:s', time());
            }
            if (empty($data['id'])) {
                $data['add_user_id'] = $this->current_user['id'];
            }

            $result = $model->update($data);
            if (!$result) {
                echo $model->getError();
            } else {
                $customModel = D('CrmCustom');
                $priority = empty($_POST['priority']) ? 0 : $_POST['priority'];
                $next_follow_time = empty($_POST['next_follow_time']) ? null : strtotime($_POST['next_follow_time']);
                $customData = array('priority' => $priority, 'next_follow_time' => $next_follow_time);
                $customModel->where('id=%d', $data['custom_id'])->save($customData);
                echo '0';
            }
        }
        exit();
    }

    public function deleteActivity()
    {
        $id = $_GET['id'];
        $model = D('CrmCustomContact');
        $result = $model->delete($id);
        if (!$result) {
            echo $model->getError();
        } else {
            echo '0';
        }
        exit();
    }

    public function activityListByPhone()
    {
        $param = $_GET['phones'];
        $phones = explode(',', $param);

        $model = D('CrmActivity');
        $result = $model->get_list_by_phone($phones);

        foreach ($result as $k => $v) {
            $result[$k]['add_user_name'] = fc_get_user_name($v['add_user_id']);
        }
        $str = json_encode($result, JSON_UNESCAPED_UNICODE);
        echo $str;

        exit();
    }

    public function allot()
    {
        $ids = $_POST['customids'];
        if (empty($ids)) {
            echo 1;
            exit();
        }
        $idarr = explode(',', $ids);
        $user_id = $_POST['selected-member-value'];
        $model = D('CrmCustom');
        $result = $model->allot($idarr, $user_id, $this->current_user);
        if ($result) {
            echo 0;
        } else {
            echo 1;
        }
        exit();
    }

    public function pick()
    {
        $id = $_POST['custom_id'];

        if (empty($id)) {
            echo 2;
            exit();
        }
        $idarr = [$id];
        $model = D('CrmCustom');
        $result = $model->allot($idarr, $this->current_user['id'], $this->current_user);
        if ($result) {
            echo 0;
        } else {
            echo 1;
        }
        exit();
    }

    public function back()
    {
        $id = $_GET['id'];
        $model = D('CrmCustom');
        $result = $model->back($id, $this->current_user);
        if ($result) {
            echo 0;
        } else {
            echo 1;
        }

        exit();
    }

    public function sign()
    {
        $id = $_GET['id'];
        $model = D('CrmCustom');
        $result = $model->sign($id, $this->current_user);
        if ($result) {
            echo 0;
        } else {
            echo 1;
        }

        exit();
    }

}